//
//  PlayViewController.m
//  BabyBedtimeStory
//
//  Created by qianfeng on 15-6-19.
//  Copyright (c) 2015年 张鹏毅. All rights reserved.
//

#import "PlayViewController.h"
#import "StoryModel.h"
#import "PlayCell.h"
#import "PlayingViewController.h"

#define kScreenSize [UIScreen mainScreen].bounds.size

@interface PlayViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArr;
@end

@implementation PlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTableView];
}

-(void)viewWillAppear:(BOOL)animated{
    NSArray *arr = [[DBManager sharedManager]readModelsWithRecordType:kDownloads];
    self.dataArr = [NSMutableArray arrayWithArray:arr];
    if (self.dataArr.count == 0) {
        [self creatAlter:@"对不起,现在没有收藏"];
    }
    [self.tableView reloadData];
}

-(void)creatTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64,kScreenSize.width, kScreenSize.height-64-49)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"PlayCell" bundle:nil] forCellReuseIdentifier:@"PlayCell"];
    [self.view addSubview:self.tableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PlayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PlayCell" forIndexPath:indexPath];
    [cell isRing:^(StoryModel *model) {
        [self.dataArr removeObject:model];
        [self.tableView reloadData];
    }];
    StoryModel *model = self.dataArr[indexPath.row];
    [cell showWithModel:model];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PlayingViewController *vc = [[PlayingViewController alloc]init];
    vc.model = self.dataArr[indexPath.row];
    vc.count = self.dataArr.count;
    vc.dataArr = self.dataArr;
    vc.SelectNum = indexPath.row;
    [self.navigationController pushViewController:vc animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
